﻿Imports System.Reflection

<ComClass(ClsAcadLayer.ClassId, ClsAcadLayer.InterfaceId, ClsAcadLayer.EventsId)>
Public Class ClsAcadLayer
    Inherits ClsAcadObject

#Region "COM GUID"
    ' 这些 GUID 提供此类的 COM 标识 
    ' 及其 COM 接口。若更改它们，则现有的
    ' 客户端将不再能访问此类。
    Public Shadows Const ClassId As String = "a191939f-d9c3-4b26-8739-e9bd9b92e096"
    Public Shadows Const InterfaceId As String = "afce298b-a89e-4351-830b-56056ef06b95"
    Public Shadows Const EventsId As String = "16263508-72b1-4566-a9f8-100182724b77"
#End Region

    ' 可创建的 COM 类必须具有一个不带参数的 Public Sub New() 
    ' 否则， 将不会在 
    ' COM 注册表中注册此类，且无法通过
    ' CreateObject 创建此类。
    Public Sub New()
        MyBase.New()
    End Sub

    Private _name As String
    Public Property Name() As String
        Get
            Return _name
        End Get
        Set(ByVal value As String)
            _name = value
        End Set
    End Property

    Private _Description As String
    Public Property Description() As String
        Get
            Return _Description
        End Get
        Set(ByVal value As String)
            _Description = value
        End Set
    End Property

    Private _layeron As Boolean
    Public Property LayerOn() As Boolean
        Get
            Return _layeron
        End Get
        Set(ByVal value As Boolean)
            _layeron = value
        End Set
    End Property

    Private _layerLock As Boolean
    Public Property Lock() As Boolean
        Get
            Return _layerLock
        End Get
        Set(ByVal value As Boolean)
            _layerLock = value
        End Set
    End Property

    Private _color As Short
    Public Property Color() As Short
        Get
            Return _color
        End Get
        Set(ByVal value As Short)
            _color = value
        End Set
    End Property

    Private _lineType As String
    Public Property LineType() As String
        Get
            Return _lineType
        End Get
        Set(ByVal value As String)
            _lineType = value
        End Set
    End Property

    Private _lineWeight As ClsAcadEntity.acLineWeight
    Public Property Lineweight() As ClsAcadEntity.acLineWeight
        Get
            Return _lineWeight
        End Get
        Set(ByVal value As ClsAcadEntity.acLineWeight)
            _lineWeight = value
        End Set
    End Property

    Public Function CreateFromAcad(layerobj As Object) As Boolean
        If TypeName(layerobj) = "AcadLayer" Then
            Dim t = Me.GetType()
            For Each item As PropertyInfo In t.GetProperties().ToList().Where(Function(c) c.CanWrite).ToList()
                If item.Name = "Color" Then
                    item.SetValue(Me, CallByName(layerobj, "TrueColor", CallType.Get, Nothing).ColorIndex, Nothing)
                Else
                    Dim val = CallByName(layerobj, item.Name, CallType.Get, Nothing)
                    If val IsNot Nothing Then
                        If val.GetType().IsCOMObject Then
                            item.SetValue(Me, val.Name, Nothing)
                        Else
                            item.SetValue(Me, val, Nothing)
                        End If
                    End If
                End If

            Next
            Return True
        Else
            Return False
        End If
    End Function

    Public Function ToCadLayer(acDoc As Object) As Object
        If TypeName(acDoc) = "AcadDocument " Then
            Dim curLayer As Object
            Try
                curLayer = acDoc.Layers.item(Me.Name)
            Catch ex As Exception
                curLayer = acDoc.Layers.Add(Me.Name)
            End Try
            curLayer.LayerOn = Me.LayerOn
            curLayer.Description = Me.Description
            curLayer.ColorIndex = Me.Color
            curLayer.Linetype = Me.LineType
            curLayer.Lineweight = Me.Lineweight
            curLayer.Lock = Me.Lock
            Return curLayer
        Else
            Return Nothing
        End If
    End Function

End Class


